package com.example.websocket_demo.controller.message;

import com.example.websocket_demo.service.message.MessageService;
import com.example.websocket_demo.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("message")
public class MessageController {
    @Autowired
    private MessageService messageService;
    @Autowired
    private UserService userService;
    /*
    * 查询消息（根据发送者id，接受者id，组id，类型查询）
    * */
    @PostMapping("selectMessage")
    public List<Map<String,Object>> selectMessage(@RequestBody Map<String, Object> messageMap) {
        return messageService.selectMessage(messageMap);
    }
    @PostMapping("selectMessageBySidOrRid")
    public List<Map<String,Object>> selectMessageBySidOrRid(@RequestBody Map<String, Object> messageMap) {
        List<Map<String,Object>> list=messageService.selectMessageBySidOrRid(messageMap);
        for(Map<String,Object> i:list){
            Map<String, Object> map=new HashMap<>();
            map.put("id",i.get("send_user_id"));
            i.put("sendUserInfo",userService.selectUser(map).get(0));
        }
        return list;
    }

    @PostMapping("delete")
    public int deleteMessage(@RequestBody Map<String, Object> messageMap) {
        return messageService.deleteMessage((Integer) messageMap.get("id"));
    }
    /**
     * 1.@RequestPart这个注解用在multipart/form-data表单提交请求的方法上。
     * 2.支持的请求方法的方式MultipartFile，属于Spring的MultipartResolver类。这个请求是通过http协议传输的。
     * 3.@RequestParam也同样支持multipart/form-data请求。
     * 4.他们最大的不同是，当请求方法的请求参数类型不再是String类型的时候。
     * 5.@RequestParam适用于name-valueString类型的请求域，@RequestPart适用于复杂的请求域（像JSON，XML,当然前端需要对应的格式的Blob对象）。
     */
    @PostMapping(value = "update")
    public int updateMessage(@RequestPart(value = "messageMap",required = false) Map<String, Object> messageMap, @RequestParam(value = "file",required = false) MultipartFile file) {
//        return messageService.updateMessage(messageMap,file);
        return 0;
    }

    @GetMapping("/message")
    public List<Map<String, Object>> selectMessages() {
        return messageService.selectMessages();
    }
}
